import re
import json
from typing import Dict, Any, Optional

class CommandParser:
    def __init__(self):
        # 定义命令映射表
        self.command_map = {
            'open_app': {
                'keywords': ['打开', '启动', '运行', '播放', '音乐', '浏览器', '笔记本', '文件', '管理器'],
                'targets': ['chrome', 'word', 'excel', 'calculator', 'notepad', 
                           'cloudmusic', 'vscode', 'wechat', 'explorer', 'photoshop',
                           'steam', 'qq', 'dingtalk', 'wps', 'ppt', 'access',
                           'outlook', 'thunder', 'bilibili', 'spider', 'teamviewer',
                           'vmware', 'virtualbox', 'docker', 'postman', 'xshell',
                           'xftp', 'winscp', '7zip', 'winrar', 'foxit', 'adobe',
                           'premiere', 'aftereffects', 'illustrator', 'sketchbook',
                           'figma', 'obs', 'potplayer', 'vlc', 'gimp', 'inkscape',
                           'vs', 'androidstudio', 'pycharm', 'idea', 'navicat',
                           'mysqlworkbench', 'redisdesktop', 'mongodb', 'git',
                           'githubdesktop', 'sourcetree', 'tortoisegit']
            },
            'close_app': {
                'keywords': ['关闭', '退出', '停止'],
                'targets': ['chrome', 'word', 'excel', 'calculator', 'notepad',
                           'cloudmusic', 'vscode', 'wechat', 'photoshop', 
                           'steam', 'terminal', 'explorer', 'qq', 'dingtalk',
                           'wps', 'ppt', 'access', 'outlook', 'thunder', 'bilibili',
                           'spider', 'teamviewer', 'vmware', 'virtualbox', 'docker',
                           'postman', 'xshell', 'xftp', 'winscp', '7zip', 'winrar',
                           'foxit', 'adobe', 'premiere', 'aftereffects', 'illustrator',
                           'sketchbook', 'figma', 'obs', 'potplayer', 'vlc', 'gimp',
                           'inkscape', 'vs', 'androidstudio', 'pycharm', 'idea',
                           'navicat', 'mysqlworkbench', 'redisdesktop', 'mongodb',
                           'git', 'githubdesktop', 'sourcetree', 'tortoisegit']
            },
            'system_cmd': {
                'keywords': ['显示', '查看', '打开', '启动'],
                'targets': ['ip', 'task_manager', 'control.exe', 'devmgmt.msc',
                           'services.msc', 'cmd', 'powershell', 'registry']
            },
            'sys_volume': {
                'keywords': ['音量', '调大', '调小', '静音', '取消静音'],
                'targets': ['up', 'down', 'mute', 'unmute', 'toggle']
            },
            'sys_light': {
                'keywords': ['亮度', '屏幕', '调亮', '调暗'],
                'targets': ['up', 'down']
            },
            'sys_power': {
                'keywords': ['锁屏', '休眠', '关机', '重启'],
                'targets': ['lock', 'sleep', 'shutdown', 'restart', 'hibernate']
            },
            'file_operation': {
                'keywords': ['文件', '文件夹', '创建', '删除', '复制', '移动', '重命名', '查找', '搜索'],
                'targets': ['create', 'delete', 'copy', 'move', 'rename', 'find', 'search']
            },
            'web_search': {
                'keywords': ['搜索', '百度', '谷歌', '必应', '查找'],
                'targets': ['baidu', 'google', 'bing', 'search']
            },
            'media_control': {
                'keywords': ['播放', '暂停', '停止', '下一首', '上一首', '快进', '快退', '全屏', '退出全屏'],
                'targets': ['play', 'pause', 'stop', 'next', 'previous', 'forward', 'rewind', 'fullscreen', 'exit_fullscreen']
            },
            'window_management': {
                'keywords': ['窗口', '最大化', '最小化', '还原', '关闭', '切换', '桌面'],
                'targets': ['maximize', 'minimize', 'restore', 'close_window', 'switch', 'desktop', 'show_desktop']
            },
            'network_operation': {
                'keywords': ['网络', '连接', '断开', 'wifi', '蓝牙', '热点'],
                'targets': ['connect', 'disconnect', 'wifi', 'bluetooth', 'hotspot']
            },
            'screenshot': {
                'keywords': ['截图', '截屏', '屏幕截图', '区域截图', '窗口截图'],
                'targets': ['full', 'area', 'window']
            },
            'reminder': {
                'keywords': ['提醒', '闹钟', '定时', '倒计时'],
                'targets': ['set', 'cancel', 'list']
            },
            'clipboard': {
                'keywords': ['剪贴板', '复制', '粘贴', '清空'],
                'targets': ['copy', 'paste', 'clear', 'show']
            },
            'text_input': {
                'keywords': ['输入', '打字', '文本'],
                'targets': ['type', 'clear', 'delete']
            }
        }
        
        # 构建关键词到命令类型的反向映射
        self.keyword_to_command = {}
        for cmd_type, config in self.command_map.items():
            for keyword in config['keywords']:
                if keyword not in self.keyword_to_command:
                    self.keyword_to_command[keyword] = []
                self.keyword_to_command[keyword].append(cmd_type)
    
    def parse_command(self, text: str) -> Dict[str, Any]:
        """
        解析用户指令
        """
        # 默认返回结构
        result = {
            "command": "",
            "target": "",
            "value": "",
            "supported": False,
            "reason": ""
        }
        
        if not text or not text.strip():
            result["reason"] = "输入指令为空"
            return result
        
        text = text.strip().lower()
        
        # 尝试解析各种命令类型（注意顺序，更具体的命令优先）
        parsed_result = (self._parse_web_search(text) or
                        self._parse_media_control(text) or
                        self._parse_window_management(text) or
                        self._parse_network_operation(text) or
                        self._parse_screenshot(text) or
                        self._parse_reminder(text) or
                        self._parse_clipboard(text) or
                        self._parse_text_input(text) or
                        self._parse_app_command(text) or 
                        self._parse_system_command(text) or 
                        self._parse_volume_command(text) or 
                        self._parse_light_command(text) or 
                        self._parse_power_command(text) or
                        self._parse_file_operation(text))
        
        if parsed_result:
            result.update(parsed_result)
            result["supported"] = True
        else:
            result["reason"] = "无法识别的指令"
        
        return result
    
    def _parse_app_command(self, text: str) -> Optional[Dict[str, Any]]:
        """解析应用程序相关命令"""
        # 应用程序别名映射
        app_aliases = {
            # 浏览器
            'chrome': 'chrome', '浏览器': 'chrome', '谷歌': 'chrome', '谷歌浏览器': 'chrome', 'edge': 'chrome',
            # 办公软件
            'notepad': 'notepad', '笔记本': 'notepad', '记事本': 'notepad', '写字板': 'notepad',
            'word': 'word', '文档': 'word', '微软文档': 'word',
            'excel': 'excel', '表格': 'excel', '微软表格': 'excel',
            'ppt': 'ppt', '幻灯片': 'ppt', '演示文稿': 'ppt',
            'wps': 'wps', 'wps办公': 'wps',
            'access': 'access', '数据库': 'access',
            'outlook': 'outlook', '邮箱': 'outlook', '邮件': 'outlook',
            # 开发工具
            'vscode': 'vscode', '代码编辑器': 'vscode', 'vs': 'vs', 'visualstudio': 'vs',
            'pycharm': 'pycharm', 'idea': 'idea', 'androidstudio': 'androidstudio',
            'postman': 'postman', 'xshell': 'xshell', 'xftp': 'xftp',
            'navicat': 'navicat', 'mysqlworkbench': 'mysqlworkbench',
            'git': 'git', 'githubdesktop': 'git', 'sourcetree': 'sourcetree',
            'tortoisegit': 'tortoisegit',
            # 系统工具
            '计算器': 'calculator', '文件管理器': 'explorer', '资源管理器': 'explorer',
            'explorer': 'explorer', 'cmd': 'cmd', '命令行': 'cmd', '终端': 'cmd',
            'powershell': 'powershell', '注册表': 'registry',
            '任务管理器': 'task_manager', '控制面板': 'control.exe',
            '设备管理器': 'devmgmt.msc', '服务': 'services.msc',
            # 压缩软件
            '7zip': '7zip', 'winrar': 'winrar', '压缩': '7zip',
            # 设计软件
            'photoshop': 'photoshop', 'ps': 'photoshop', 'adobe': 'photoshop',
            'premiere': 'premiere', 'pr': 'premiere', 'aftereffects': 'aftereffects',
            'ae': 'aftereffects', 'illustrator': 'illustrator', 'ai': 'illustrator',
            'sketchbook': 'sketchbook', 'figma': 'figma', 'gimp': 'gimp',
            'inkscape': 'inkscape',
            # 虚拟化
            'vmware': 'vmware', 'virtualbox': 'virtualbox', 'docker': 'docker',
            # 远程工具
            'teamviewer': 'teamviewer', '远程桌面': 'teamviewer',
            # 下载工具
            'thunder': 'thunder', '迅雷': 'thunder',
            # 媒体播放
            'potplayer': 'potplayer', 'vlc': 'vlc', '播放器': 'potplayer',
            'obs': 'obs', '录屏': 'obs', '直播': 'obs',
            # 社交软件
            '微信': 'wechat', 'qq': 'qq', '钉钉': 'dingtalk',
            # 音乐
            '音乐': 'cloudmusic', '网易云音乐': 'cloudmusic', 'cloudmusic': 'cloudmusic',
            # 游戏
            'steam': 'steam', '游戏平台': 'steam',
            # 阅读器
            'foxit': 'foxit', 'pdf': 'foxit', '阅读器': 'foxit',
            # 视频
            'bilibili': 'bilibili', 'b站': 'bilibili', '视频': 'bilibili'
        }
        
        # 打开程序命令 - 支持别名和模糊匹配
        open_patterns = [
            r'(打开|启动|运行)\s*([^\s]+)',
            r'播放\s*(音乐|视频|cloudmusic|bilibili)',
            r'打开\s*(浏览器|笔记本|记事本|文件管理器|资源管理器|计算器|压缩|播放器|阅读器|pdf)',
            r'启动\s*(微信|qq|钉钉|steam|游戏)',
            r'运行\s*(vscode|vs|pycharm|idea|postman|xshell|navicat|git)'
        ]
        
        for pattern in open_patterns:
            match = re.search(pattern, text)
            if match:
                # 提取目标程序名
                target_text = match.group(2) if match.lastindex >= 2 else match.group(1)
                target_text = target_text.strip().lower()
                
                # 查找匹配的应用程序
                matched_app = None
                
                # 直接匹配
                if target_text in app_aliases:
                    matched_app = app_aliases[target_text]
                else:
                    # 模糊匹配
                    for alias, app in app_aliases.items():
                        if target_text in alias or alias in target_text:
                            matched_app = app
                            break
                
                if matched_app:
                    return {
                        "command": "open_app",
                        "target": matched_app,
                        "value": ""
                    }
        
        # 关闭程序命令
        close_patterns = [
            r'(关闭|退出|停止)\s*([^\s]+)',
            r'关闭\s*(浏览器|笔记本|记事本|文件管理器|资源管理器|计算器|压缩|播放器|阅读器|pdf)',
            r'退出\s*(微信|qq|钉钉|steam|游戏)',
            r'停止\s*(vscode|vs|pycharm|idea|postman|xshell|navicat|git)'
        ]
        
        for pattern in close_patterns:
            match = re.search(pattern, text)
            if match:
                target_text = match.group(2) if match.lastindex >= 2 else match.group(1)
                target_text = target_text.strip().lower()
                
                # 查找匹配的应用程序
                matched_app = None
                
                # 直接匹配
                if target_text in app_aliases:
                    matched_app = app_aliases[target_text]
                else:
                    # 模糊匹配
                    for alias, app in app_aliases.items():
                        if target_text in alias or alias in target_text:
                            matched_app = app
                            break
                
                if matched_app:
                    return {
                        "command": "close_app",
                        "target": matched_app,
                        "value": ""
                    }
        
        return None
    
    def _parse_file_operation(self, text: str) -> Optional[Dict[str, Any]]:
        """解析文件操作命令"""
        file_patterns = [
            r'创建\s*(文件|文件夹)\s*([^\s]+)',
            r'删除\s*(文件|文件夹)\s*([^\s]+)',
            r'复制\s*([^\s]+)\s*到\s*([^\s]+)',
            r'移动\s*([^\s]+)\s*到\s*([^\s]+)',
            r'重命名\s*([^\s]+)\s*为\s*([^\s]+)',
            r'查找\s*([^\s]+)',
            r'搜索\s*([^\s]+)'
        ]
        
        for pattern in file_patterns:
            match = re.search(pattern, text)
            if match:
                if '创建' in text:
                    target_type = match.group(1)
                    file_path = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "create",
                        "value": f"{target_type}:{file_path}"
                    }
                elif '删除' in text:
                    file_path = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "delete",
                        "value": file_path
                    }
                elif '复制' in text:
                    source = match.group(1)
                    dest = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "copy",
                        "value": f"{source}:{dest}"
                    }
                elif '移动' in text:
                    source = match.group(1)
                    dest = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "move",
                        "value": f"{source}:{dest}"
                    }
                elif '重命名' in text:
                    old_name = match.group(1)
                    new_name = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "rename",
                        "value": f"{old_name}:{new_name}"
                    }
                elif '查找' in text or '搜索' in text:
                    search_term = match.group(1)
                    return {
                        "command": "file_operation",
                        "target": "find",
                        "value": search_term
                    }
        
        return None
    
    def _parse_web_search(self, text: str) -> Optional[Dict[str, Any]]:
        """解析网络搜索命令"""
        search_patterns = [
            r'(百度|谷歌|必应)\s*搜索\s*([^\s].+)',
            r'搜索\s*([^\s].+)',
            r'查找\s*([^\s].+)'
        ]
        
        for pattern in search_patterns:
            match = re.search(pattern, text)
            if match:
                if '百度' in text:
                    search_term = match.group(2) if match.lastindex >= 2 else match.group(1)
                    return {
                        "command": "web_search",
                        "target": "baidu",
                        "value": search_term
                    }
                elif '谷歌' in text:
                    search_term = match.group(2) if match.lastindex >= 2 else match.group(1)
                    return {
                        "command": "web_search",
                        "target": "google",
                        "value": search_term
                    }
                elif '必应' in text:
                    search_term = match.group(2) if match.lastindex >= 2 else match.group(1)
                    return {
                        "command": "web_search",
                        "target": "bing",
                        "value": search_term
                    }
                else:
                    search_term = match.group(1)
                    return {
                        "command": "web_search",
                        "target": "baidu",
                        "value": search_term
                    }
        
        return None
    
    def _parse_media_control(self, text: str) -> Optional[Dict[str, Any]]:
        """解析媒体控制命令"""
        media_map = {
            '播放': 'play',
            '暂停': 'pause',
            '停止': 'stop',
            '下一首': 'next',
            '上一首': 'previous',
            '快进': 'forward',
            '快退': 'rewind',
            '全屏': 'fullscreen',
            '退出全屏': 'exit_fullscreen'
        }
        
        for keyword, action in media_map.items():
            if keyword in text:
                return {
                    "command": "media_control",
                    "target": action,
                    "value": ""
                }
        
        return None
    
    def _parse_window_management(self, text: str) -> Optional[Dict[str, Any]]:
        """解析窗口管理命令"""
        window_map = {
            '最大化': 'maximize',
            '最小化': 'minimize',
            '还原': 'restore',
            '关闭窗口': 'close_window',
            '切换': 'switch',
            '显示桌面': 'show_desktop',
            '桌面': 'show_desktop'
        }
        
        for keyword, action in window_map.items():
            if keyword in text:
                return {
                    "command": "window_management",
                    "target": action,
                    "value": ""
                }
        
        return None
    
    def _parse_network_operation(self, text: str) -> Optional[Dict[str, Any]]:
        """解析网络操作命令"""
        if 'wifi' in text:
            if '连接' in text:
                # 提取WiFi名称
                wifi_match = re.search(r'连接\s*wifi\s*([^\s]+)', text)
                wifi_name = wifi_match.group(1) if wifi_match else ""
                return {
                    "command": "network_operation",
                    "target": "wifi_connect",
                    "value": wifi_name
                }
            elif '断开' in text:
                return {
                    "command": "network_operation",
                    "target": "wifi_disconnect",
                    "value": ""
                }
        elif '蓝牙' in text:
            if '打开' in text or '开启' in text:
                return {
                    "command": "network_operation",
                    "target": "bluetooth_on",
                    "value": ""
                }
            elif '关闭' in text:
                return {
                    "command": "network_operation",
                    "target": "bluetooth_off",
                    "value": ""
                }
        elif '热点' in text:
            if '打开' in text or '开启' in text:
                return {
                    "command": "network_operation",
                    "target": "hotspot_on",
                    "value": ""
                }
            elif '关闭' in text:
                return {
                    "command": "network_operation",
                    "target": "hotspot_off",
                    "value": ""
                }
        
        return None
    
    def _parse_screenshot(self, text: str) -> Optional[Dict[str, Any]]:
        """解析截图命令"""
        if '截图' in text or '截屏' in text:
            if '区域' in text:
                return {
                    "command": "screenshot",
                    "target": "area",
                    "value": ""
                }
            elif '窗口' in text:
                return {
                    "command": "screenshot",
                    "target": "window",
                    "value": ""
                }
            else:
                return {
                    "command": "screenshot",
                    "target": "full",
                    "value": ""
                }
        
        return None
    
    def _parse_reminder(self, text: str) -> Optional[Dict[str, Any]]:
        """解析提醒命令"""
        if '提醒' in text or '闹钟' in text:
            # 提取时间和提醒内容
            time_match = re.search(r'(\d+)\s*(分钟|小时|秒)', text)
            content_match = re.search(r'提醒\s*[:：]?\s*([^\s].+)', text)
            
            if time_match:
                time_value = time_match.group(1)
                time_unit = time_match.group(2)
                content = content_match.group(1) if content_match else "提醒时间到"
                
                return {
                    "command": "reminder",
                    "target": "set",
                    "value": f"{time_value}:{time_unit}:{content}"
                }
            elif content_match:
                content = content_match.group(1)
                return {
                    "command": "reminder",
                    "target": "set",
                    "value": f"0:分钟:{content}"
                }
        elif '取消提醒' in text:
            return {
                "command": "reminder",
                "target": "cancel",
                "value": ""
            }
        elif '查看提醒' in text:
            return {
                "command": "reminder",
                "target": "list",
                "value": ""
            }
        
        return None
    
    def _parse_clipboard(self, text: str) -> Optional[Dict[str, Any]]:
        """解析剪贴板命令"""
        if '剪贴板' in text:
            if '清空' in text or '清除' in text:
                return {
                    "command": "clipboard",
                    "target": "clear",
                    "value": ""
                }
            elif '显示' in text or '查看' in text:
                return {
                    "command": "clipboard",
                    "target": "show",
                    "value": ""
                }
        elif '复制' in text:
            # 提取要复制的内容
            content_match = re.search(r'复制\s*[:：]?\s*([^\s].+)', text)
            if content_match:
                content = content_match.group(1)
                return {
                    "command": "clipboard",
                    "target": "copy",
                    "value": content
                }
        elif '粘贴' in text:
            return {
                "command": "clipboard",
                "target": "paste",
                "value": ""
            }
        
        return None
    
    def _parse_text_input(self, text: str) -> Optional[Dict[str, Any]]:
        """解析文本输入命令"""
        if '输入' in text or '打字' in text:
            # 提取要输入的文本
            content_match = re.search(r'(输入|打字)\s*[:：]?\s*([^\s].+)', text)
            if content_match:
                content = content_match.group(2)
                return {
                    "command": "text_input",
                    "target": "type",
                    "value": content
                }
        elif '清空' in text and ('输入' in text or '文本' in text):
            return {
                "command": "text_input",
                "target": "clear",
                "value": ""
            }
        elif '删除' in text and ('文本' in text or '输入' in text):
            return {
                "command": "text_input",
                "target": "delete",
                "value": ""
            }
        
        return None
    
    def _parse_system_command(self, text: str) -> Optional[Dict[str, Any]]:
        """解析系统命令"""
        system_patterns = [
            r'(显示|查看)\s*ip',
            r'(打开|启动)\s*(任务管理器|task_manager)',
            r'(打开|启动)\s*(控制面板|control\.exe)',
            r'(打开|启动)\s*(设备管理器|devmgmt\.msc)',
            r'(打开|启动)\s*(服务|services\.msc)',
            r'(打开|启动)\s*(cmd|命令行|终端)',
            r'(打开|启动)\s*(powershell|powershell)',
            r'(打开|启动)\s*(注册表|registry)'
        ]
        
        target_map = {
            'ip': 'ip',
            '任务管理器': 'task_manager', 'task_manager': 'task_manager',
            '控制面板': 'control.exe', 'control.exe': 'control.exe',
            '设备管理器': 'devmgmt.msc', 'devmgmt.msc': 'devmgmt.msc',
            '服务': 'services.msc', 'services.msc': 'services.msc',
            'cmd': 'cmd', '命令行': 'cmd', '终端': 'cmd',
            'powershell': 'powershell',
            '注册表': 'registry', 'registry': 'registry'
        }
        
        for pattern in system_patterns:
            match = re.search(pattern, text)
            if match:
                target_text = match.group(2) if match.lastindex >= 2 else match.group(1)
                target = target_map.get(target_text, target_text)
                return {
                    "command": "system_cmd",
                    "target": target,
                    "value": ""
                }
        
        return None
    
    def _parse_file_operation(self, text: str) -> Optional[Dict[str, Any]]:
        """解析文件操作命令"""
        file_patterns = [
            r'创建\s*(文件|文件夹)\s*([^\s]+)',
            r'删除\s*(文件|文件夹)\s*([^\s]+)',
            r'复制\s*([^\s]+)\s*到\s*([^\s]+)',
            r'移动\s*([^\s]+)\s*到\s*([^\s]+)',
            r'重命名\s*([^\s]+)\s*为\s*([^\s]+)',
            r'查找\s*([^\s]+)',
            r'搜索\s*([^\s]+)'
        ]
        
        for pattern in file_patterns:
            match = re.search(pattern, text)
            if match:
                if '创建' in text:
                    target_type = match.group(1)
                    file_path = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "create",
                        "value": f"{target_type}:{file_path}"
                    }
                elif '删除' in text:
                    file_path = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "delete",
                        "value": file_path
                    }
                elif '复制' in text:
                    source = match.group(1)
                    dest = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "copy",
                        "value": f"{source}:{dest}"
                    }
                elif '移动' in text:
                    source = match.group(1)
                    dest = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "move",
                        "value": f"{source}:{dest}"
                    }
                elif '重命名' in text:
                    old_name = match.group(1)
                    new_name = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "rename",
                        "value": f"{old_name}:{new_name}"
                    }
                elif '查找' in text or '搜索' in text:
                    search_term = match.group(1)
                    return {
                        "command": "file_operation",
                        "target": "find",
                        "value": search_term
                    }
        
        return None
    
    def _parse_web_search(self, text: str) -> Optional[Dict[str, Any]]:
        """解析网络搜索命令"""
        search_patterns = [
            r'(百度|谷歌|必应)\s*搜索\s*([^\s].+)',
            r'搜索\s*([^\s].+)',
            r'查找\s*([^\s].+)'
        ]
        
        for pattern in search_patterns:
            match = re.search(pattern, text)
            if match:
                if '百度' in text:
                    search_term = match.group(2) if match.lastindex >= 2 else match.group(1)
                    return {
                        "command": "web_search",
                        "target": "baidu",
                        "value": search_term
                    }
                elif '谷歌' in text:
                    search_term = match.group(2) if match.lastindex >= 2 else match.group(1)
                    return {
                        "command": "web_search",
                        "target": "google",
                        "value": search_term
                    }
                elif '必应' in text:
                    search_term = match.group(2) if match.lastindex >= 2 else match.group(1)
                    return {
                        "command": "web_search",
                        "target": "bing",
                        "value": search_term
                    }
                else:
                    search_term = match.group(1)
                    return {
                        "command": "web_search",
                        "target": "baidu",
                        "value": search_term
                    }
        
        return None
    
    def _parse_media_control(self, text: str) -> Optional[Dict[str, Any]]:
        """解析媒体控制命令"""
        media_map = {
            '播放': 'play',
            '暂停': 'pause',
            '停止': 'stop',
            '下一首': 'next',
            '上一首': 'previous',
            '快进': 'forward',
            '快退': 'rewind',
            '全屏': 'fullscreen',
            '退出全屏': 'exit_fullscreen'
        }
        
        for keyword, action in media_map.items():
            if keyword in text:
                return {
                    "command": "media_control",
                    "target": action,
                    "value": ""
                }
        
        return None
    
    def _parse_window_management(self, text: str) -> Optional[Dict[str, Any]]:
        """解析窗口管理命令"""
        window_map = {
            '最大化': 'maximize',
            '最小化': 'minimize',
            '还原': 'restore',
            '关闭窗口': 'close_window',
            '切换': 'switch',
            '显示桌面': 'show_desktop',
            '桌面': 'show_desktop'
        }
        
        for keyword, action in window_map.items():
            if keyword in text:
                return {
                    "command": "window_management",
                    "target": action,
                    "value": ""
                }
        
        return None
    
    def _parse_network_operation(self, text: str) -> Optional[Dict[str, Any]]:
        """解析网络操作命令"""
        if 'wifi' in text:
            if '连接' in text:
                # 提取WiFi名称
                wifi_match = re.search(r'连接\s*wifi\s*([^\s]+)', text)
                wifi_name = wifi_match.group(1) if wifi_match else ""
                return {
                    "command": "network_operation",
                    "target": "wifi_connect",
                    "value": wifi_name
                }
            elif '断开' in text:
                return {
                    "command": "network_operation",
                    "target": "wifi_disconnect",
                    "value": ""
                }
        elif '蓝牙' in text:
            if '打开' in text or '开启' in text:
                return {
                    "command": "network_operation",
                    "target": "bluetooth_on",
                    "value": ""
                }
            elif '关闭' in text:
                return {
                    "command": "network_operation",
                    "target": "bluetooth_off",
                    "value": ""
                }
        elif '热点' in text:
            if '打开' in text or '开启' in text:
                return {
                    "command": "network_operation",
                    "target": "hotspot_on",
                    "value": ""
                }
            elif '关闭' in text:
                return {
                    "command": "network_operation",
                    "target": "hotspot_off",
                    "value": ""
                }
        
        return None
    
    def _parse_screenshot(self, text: str) -> Optional[Dict[str, Any]]:
        """解析截图命令"""
        if '截图' in text or '截屏' in text:
            if '区域' in text:
                return {
                    "command": "screenshot",
                    "target": "area",
                    "value": ""
                }
            elif '窗口' in text:
                return {
                    "command": "screenshot",
                    "target": "window",
                    "value": ""
                }
            else:
                return {
                    "command": "screenshot",
                    "target": "full",
                    "value": ""
                }
        
        return None
    
    def _parse_reminder(self, text: str) -> Optional[Dict[str, Any]]:
        """解析提醒命令"""
        if '提醒' in text or '闹钟' in text:
            # 提取时间和提醒内容
            time_match = re.search(r'(\d+)\s*(分钟|小时|秒)', text)
            content_match = re.search(r'提醒\s*[:：]?\s*([^\s].+)', text)
            
            if time_match:
                time_value = time_match.group(1)
                time_unit = time_match.group(2)
                content = content_match.group(1) if content_match else "提醒时间到"
                
                return {
                    "command": "reminder",
                    "target": "set",
                    "value": f"{time_value}:{time_unit}:{content}"
                }
            elif content_match:
                content = content_match.group(1)
                return {
                    "command": "reminder",
                    "target": "set",
                    "value": f"0:分钟:{content}"
                }
        elif '取消提醒' in text:
            return {
                "command": "reminder",
                "target": "cancel",
                "value": ""
            }
        elif '查看提醒' in text:
            return {
                "command": "reminder",
                "target": "list",
                "value": ""
            }
        
        return None
    
    def _parse_clipboard(self, text: str) -> Optional[Dict[str, Any]]:
        """解析剪贴板命令"""
        if '剪贴板' in text:
            if '清空' in text or '清除' in text:
                return {
                    "command": "clipboard",
                    "target": "clear",
                    "value": ""
                }
            elif '显示' in text or '查看' in text:
                return {
                    "command": "clipboard",
                    "target": "show",
                    "value": ""
                }
        elif '复制' in text:
            # 提取要复制的内容
            content_match = re.search(r'复制\s*[:：]?\s*([^\s].+)', text)
            if content_match:
                content = content_match.group(1)
                return {
                    "command": "clipboard",
                    "target": "copy",
                    "value": content
                }
        elif '粘贴' in text:
            return {
                "command": "clipboard",
                "target": "paste",
                "value": ""
            }
        
        return None
    
    def _parse_text_input(self, text: str) -> Optional[Dict[str, Any]]:
        """解析文本输入命令"""
        if '输入' in text or '打字' in text:
            # 提取要输入的文本
            content_match = re.search(r'(输入|打字)\s*[:：]?\s*([^\s].+)', text)
            if content_match:
                content = content_match.group(2)
                return {
                    "command": "text_input",
                    "target": "type",
                    "value": content
                }
        elif '清空' in text and ('输入' in text or '文本' in text):
            return {
                "command": "text_input",
                "target": "clear",
                "value": ""
            }
        elif '删除' in text and ('文本' in text or '输入' in text):
            return {
                "command": "text_input",
                "target": "delete",
                "value": ""
            }
        
        return None
    
    def _parse_volume_command(self, text: str) -> Optional[Dict[str, Any]]:
        """解析音量调节命令"""
        
        if '音量' in text or '静音' in text or any(keyword in text for keyword in ['调大', '调小']):
            if any(keyword in text for keyword in ['调大', '增大', '增加']):
                return {"command": "sys_volume", "target": "up", "value": "10"}
            elif any(keyword in text for keyword in ['调小', '减小', '降低']):
                return {"command": "sys_volume", "target": "down", "value": "10"}
            elif any(keyword in text for keyword in ['静音', '关闭声音']):
                return {"command": "sys_volume", "target": "mute", "value": ""}
            elif any(keyword in text for keyword in ['取消静音', '开启声音', '打开声音']):
                return {"command": "sys_volume", "target": "unmute", "value": ""}
            else:
                return {"command": "sys_volume", "target": "toggle", "value": ""}
        
        return None
    
    def _parse_file_operation(self, text: str) -> Optional[Dict[str, Any]]:
        """解析文件操作命令"""
        file_patterns = [
            r'创建\s*(文件|文件夹)\s*([^\s]+)',
            r'删除\s*(文件|文件夹)\s*([^\s]+)',
            r'复制\s*([^\s]+)\s*到\s*([^\s]+)',
            r'移动\s*([^\s]+)\s*到\s*([^\s]+)',
            r'重命名\s*([^\s]+)\s*为\s*([^\s]+)',
            r'查找\s*([^\s]+)',
            r'搜索\s*([^\s]+)'
        ]
        
        for pattern in file_patterns:
            match = re.search(pattern, text)
            if match:
                if '创建' in text:
                    target_type = match.group(1)
                    file_path = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "create",
                        "value": f"{target_type}:{file_path}"
                    }
                elif '删除' in text:
                    file_path = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "delete",
                        "value": file_path
                    }
                elif '复制' in text:
                    source = match.group(1)
                    dest = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "copy",
                        "value": f"{source}:{dest}"
                    }
                elif '移动' in text:
                    source = match.group(1)
                    dest = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "move",
                        "value": f"{source}:{dest}"
                    }
                elif '重命名' in text:
                    old_name = match.group(1)
                    new_name = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "rename",
                        "value": f"{old_name}:{new_name}"
                    }
                elif '查找' in text or '搜索' in text:
                    search_term = match.group(1)
                    return {
                        "command": "file_operation",
                        "target": "find",
                        "value": search_term
                    }
        
        return None
    
    def _parse_web_search(self, text: str) -> Optional[Dict[str, Any]]:
        """解析网络搜索命令"""
        search_patterns = [
            r'(百度|谷歌|必应)\s*搜索\s*([^\s].+)',
            r'搜索\s*([^\s].+)',
            r'查找\s*([^\s].+)'
        ]
        
        for pattern in search_patterns:
            match = re.search(pattern, text)
            if match:
                if '百度' in text:
                    search_term = match.group(2) if match.lastindex >= 2 else match.group(1)
                    return {
                        "command": "web_search",
                        "target": "baidu",
                        "value": search_term
                    }
                elif '谷歌' in text:
                    search_term = match.group(2) if match.lastindex >= 2 else match.group(1)
                    return {
                        "command": "web_search",
                        "target": "google",
                        "value": search_term
                    }
                elif '必应' in text:
                    search_term = match.group(2) if match.lastindex >= 2 else match.group(1)
                    return {
                        "command": "web_search",
                        "target": "bing",
                        "value": search_term
                    }
                else:
                    search_term = match.group(1)
                    return {
                        "command": "web_search",
                        "target": "baidu",
                        "value": search_term
                    }
        
        return None
    
    def _parse_media_control(self, text: str) -> Optional[Dict[str, Any]]:
        """解析媒体控制命令"""
        media_map = {
            '播放': 'play',
            '暂停': 'pause',
            '停止': 'stop',
            '下一首': 'next',
            '上一首': 'previous',
            '快进': 'forward',
            '快退': 'rewind',
            '全屏': 'fullscreen',
            '退出全屏': 'exit_fullscreen'
        }
        
        for keyword, action in media_map.items():
            if keyword in text:
                return {
                    "command": "media_control",
                    "target": action,
                    "value": ""
                }
        
        return None
    
    def _parse_window_management(self, text: str) -> Optional[Dict[str, Any]]:
        """解析窗口管理命令"""
        window_map = {
            '最大化': 'maximize',
            '最小化': 'minimize',
            '还原': 'restore',
            '关闭窗口': 'close_window',
            '切换': 'switch',
            '显示桌面': 'show_desktop',
            '桌面': 'show_desktop'
        }
        
        for keyword, action in window_map.items():
            if keyword in text:
                return {
                    "command": "window_management",
                    "target": action,
                    "value": ""
                }
        
        return None
    
    def _parse_network_operation(self, text: str) -> Optional[Dict[str, Any]]:
        """解析网络操作命令"""
        if 'wifi' in text:
            if '连接' in text:
                # 提取WiFi名称
                wifi_match = re.search(r'连接\s*wifi\s*([^\s]+)', text)
                wifi_name = wifi_match.group(1) if wifi_match else ""
                return {
                    "command": "network_operation",
                    "target": "wifi_connect",
                    "value": wifi_name
                }
            elif '断开' in text:
                return {
                    "command": "network_operation",
                    "target": "wifi_disconnect",
                    "value": ""
                }
        elif '蓝牙' in text:
            if '打开' in text or '开启' in text:
                return {
                    "command": "network_operation",
                    "target": "bluetooth_on",
                    "value": ""
                }
            elif '关闭' in text:
                return {
                    "command": "network_operation",
                    "target": "bluetooth_off",
                    "value": ""
                }
        elif '热点' in text:
            if '打开' in text or '开启' in text:
                return {
                    "command": "network_operation",
                    "target": "hotspot_on",
                    "value": ""
                }
            elif '关闭' in text:
                return {
                    "command": "network_operation",
                    "target": "hotspot_off",
                    "value": ""
                }
        
        return None
    
    def _parse_screenshot(self, text: str) -> Optional[Dict[str, Any]]:
        """解析截图命令"""
        if '截图' in text or '截屏' in text:
            if '区域' in text:
                return {
                    "command": "screenshot",
                    "target": "area",
                    "value": ""
                }
            elif '窗口' in text:
                return {
                    "command": "screenshot",
                    "target": "window",
                    "value": ""
                }
            else:
                return {
                    "command": "screenshot",
                    "target": "full",
                    "value": ""
                }
        
        return None
    
    def _parse_reminder(self, text: str) -> Optional[Dict[str, Any]]:
        """解析提醒命令"""
        if '提醒' in text or '闹钟' in text:
            # 提取时间和提醒内容
            time_match = re.search(r'(\d+)\s*(分钟|小时|秒)', text)
            content_match = re.search(r'提醒\s*[:：]?\s*([^\s].+)', text)
            
            if time_match:
                time_value = time_match.group(1)
                time_unit = time_match.group(2)
                content = content_match.group(1) if content_match else "提醒时间到"
                
                return {
                    "command": "reminder",
                    "target": "set",
                    "value": f"{time_value}:{time_unit}:{content}"
                }
            elif content_match:
                content = content_match.group(1)
                return {
                    "command": "reminder",
                    "target": "set",
                    "value": f"0:分钟:{content}"
                }
        elif '取消提醒' in text:
            return {
                "command": "reminder",
                "target": "cancel",
                "value": ""
            }
        elif '查看提醒' in text:
            return {
                "command": "reminder",
                "target": "list",
                "value": ""
            }
        
        return None
    
    def _parse_clipboard(self, text: str) -> Optional[Dict[str, Any]]:
        """解析剪贴板命令"""
        if '剪贴板' in text:
            if '清空' in text or '清除' in text:
                return {
                    "command": "clipboard",
                    "target": "clear",
                    "value": ""
                }
            elif '显示' in text or '查看' in text:
                return {
                    "command": "clipboard",
                    "target": "show",
                    "value": ""
                }
        elif '复制' in text:
            # 提取要复制的内容
            content_match = re.search(r'复制\s*[:：]?\s*([^\s].+)', text)
            if content_match:
                content = content_match.group(1)
                return {
                    "command": "clipboard",
                    "target": "copy",
                    "value": content
                }
        elif '粘贴' in text:
            return {
                "command": "clipboard",
                "target": "paste",
                "value": ""
            }
        
        return None
    
    def _parse_text_input(self, text: str) -> Optional[Dict[str, Any]]:
        """解析文本输入命令"""
        if '输入' in text or '打字' in text:
            # 提取要输入的文本
            content_match = re.search(r'(输入|打字)\s*[:：]?\s*([^\s].+)', text)
            if content_match:
                content = content_match.group(2)
                return {
                    "command": "text_input",
                    "target": "type",
                    "value": content
                }
        elif '清空' in text and ('输入' in text or '文本' in text):
            return {
                "command": "text_input",
                "target": "clear",
                "value": ""
            }
        elif '删除' in text and ('文本' in text or '输入' in text):
            return {
                "command": "text_input",
                "target": "delete",
                "value": ""
            }
        
        return None
    
    def _parse_light_command(self, text: str) -> Optional[Dict[str, Any]]:
        """解析亮度调节命令"""
        if '亮度' in text or '屏幕' in text:
            # 提取百分比数值
            percent_match = re.search(r'(\d+)%', text)
            percent_value = percent_match.group(1) if percent_match else "10"
            
            if any(keyword in text for keyword in ['调亮', '增加亮度', '提高亮度']):
                return {"command": "sys_light", "target": "up", "value": percent_value}
            elif any(keyword in text for keyword in ['调暗', '降低亮度', '减少亮度']):
                return {"command": "sys_light", "target": "down", "value": percent_value}
            else:
                # 默认调亮
                return {"command": "sys_light", "target": "up", "value": percent_value}
        
        return None
    
    def _parse_file_operation(self, text: str) -> Optional[Dict[str, Any]]:
        """解析文件操作命令"""
        file_patterns = [
            r'创建\s*(文件|文件夹)\s*([^\s]+)',
            r'删除\s*(文件|文件夹)\s*([^\s]+)',
            r'复制\s*([^\s]+)\s*到\s*([^\s]+)',
            r'移动\s*([^\s]+)\s*到\s*([^\s]+)',
            r'重命名\s*([^\s]+)\s*为\s*([^\s]+)',
            r'查找\s*([^\s]+)',
            r'搜索\s*([^\s]+)'
        ]
        
        for pattern in file_patterns:
            match = re.search(pattern, text)
            if match:
                if '创建' in text:
                    target_type = match.group(1)
                    file_path = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "create",
                        "value": f"{target_type}:{file_path}"
                    }
                elif '删除' in text:
                    file_path = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "delete",
                        "value": file_path
                    }
                elif '复制' in text:
                    source = match.group(1)
                    dest = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "copy",
                        "value": f"{source}:{dest}"
                    }
                elif '移动' in text:
                    source = match.group(1)
                    dest = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "move",
                        "value": f"{source}:{dest}"
                    }
                elif '重命名' in text:
                    old_name = match.group(1)
                    new_name = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "rename",
                        "value": f"{old_name}:{new_name}"
                    }
                elif '查找' in text or '搜索' in text:
                    search_term = match.group(1)
                    return {
                        "command": "file_operation",
                        "target": "find",
                        "value": search_term
                    }
        
        return None
    
    def _parse_web_search(self, text: str) -> Optional[Dict[str, Any]]:
        """解析网络搜索命令"""
        search_patterns = [
            r'(百度|谷歌|必应)\s*搜索\s*([^\s].+)',
            r'搜索\s*([^\s].+)',
            r'查找\s*([^\s].+)'
        ]
        
        for pattern in search_patterns:
            match = re.search(pattern, text)
            if match:
                if '百度' in text:
                    search_term = match.group(2) if match.lastindex >= 2 else match.group(1)
                    return {
                        "command": "web_search",
                        "target": "baidu",
                        "value": search_term
                    }
                elif '谷歌' in text:
                    search_term = match.group(2) if match.lastindex >= 2 else match.group(1)
                    return {
                        "command": "web_search",
                        "target": "google",
                        "value": search_term
                    }
                elif '必应' in text:
                    search_term = match.group(2) if match.lastindex >= 2 else match.group(1)
                    return {
                        "command": "web_search",
                        "target": "bing",
                        "value": search_term
                    }
                else:
                    search_term = match.group(1)
                    return {
                        "command": "web_search",
                        "target": "baidu",
                        "value": search_term
                    }
        
        return None
    
    def _parse_media_control(self, text: str) -> Optional[Dict[str, Any]]:
        """解析媒体控制命令"""
        media_map = {
            '播放': 'play',
            '暂停': 'pause',
            '停止': 'stop',
            '下一首': 'next',
            '上一首': 'previous',
            '快进': 'forward',
            '快退': 'rewind',
            '全屏': 'fullscreen',
            '退出全屏': 'exit_fullscreen'
        }
        
        for keyword, action in media_map.items():
            if keyword in text:
                return {
                    "command": "media_control",
                    "target": action,
                    "value": ""
                }
        
        return None
    
    def _parse_window_management(self, text: str) -> Optional[Dict[str, Any]]:
        """解析窗口管理命令"""
        window_map = {
            '最大化': 'maximize',
            '最小化': 'minimize',
            '还原': 'restore',
            '关闭窗口': 'close_window',
            '切换': 'switch',
            '显示桌面': 'show_desktop',
            '桌面': 'show_desktop'
        }
        
        for keyword, action in window_map.items():
            if keyword in text:
                return {
                    "command": "window_management",
                    "target": action,
                    "value": ""
                }
        
        return None
    
    def _parse_network_operation(self, text: str) -> Optional[Dict[str, Any]]:
        """解析网络操作命令"""
        if 'wifi' in text:
            if '连接' in text:
                # 提取WiFi名称
                wifi_match = re.search(r'连接\s*wifi\s*([^\s]+)', text)
                wifi_name = wifi_match.group(1) if wifi_match else ""
                return {
                    "command": "network_operation",
                    "target": "wifi_connect",
                    "value": wifi_name
                }
            elif '断开' in text:
                return {
                    "command": "network_operation",
                    "target": "wifi_disconnect",
                    "value": ""
                }
        elif '蓝牙' in text:
            if '打开' in text or '开启' in text:
                return {
                    "command": "network_operation",
                    "target": "bluetooth_on",
                    "value": ""
                }
            elif '关闭' in text:
                return {
                    "command": "network_operation",
                    "target": "bluetooth_off",
                    "value": ""
                }
        elif '热点' in text:
            if '打开' in text or '开启' in text:
                return {
                    "command": "network_operation",
                    "target": "hotspot_on",
                    "value": ""
                }
            elif '关闭' in text:
                return {
                    "command": "network_operation",
                    "target": "hotspot_off",
                    "value": ""
                }
        
        return None
    
    def _parse_screenshot(self, text: str) -> Optional[Dict[str, Any]]:
        """解析截图命令"""
        if '截图' in text or '截屏' in text:
            if '区域' in text:
                return {
                    "command": "screenshot",
                    "target": "area",
                    "value": ""
                }
            elif '窗口' in text:
                return {
                    "command": "screenshot",
                    "target": "window",
                    "value": ""
                }
            else:
                return {
                    "command": "screenshot",
                    "target": "full",
                    "value": ""
                }
        
        return None
    
    def _parse_reminder(self, text: str) -> Optional[Dict[str, Any]]:
        """解析提醒命令"""
        if '提醒' in text or '闹钟' in text:
            # 提取时间和提醒内容
            time_match = re.search(r'(\d+)\s*(分钟|小时|秒)', text)
            content_match = re.search(r'提醒\s*[:：]?\s*([^\s].+)', text)
            
            if time_match:
                time_value = time_match.group(1)
                time_unit = time_match.group(2)
                content = content_match.group(1) if content_match else "提醒时间到"
                
                return {
                    "command": "reminder",
                    "target": "set",
                    "value": f"{time_value}:{time_unit}:{content}"
                }
            elif content_match:
                content = content_match.group(1)
                return {
                    "command": "reminder",
                    "target": "set",
                    "value": f"0:分钟:{content}"
                }
        elif '取消提醒' in text:
            return {
                "command": "reminder",
                "target": "cancel",
                "value": ""
            }
        elif '查看提醒' in text:
            return {
                "command": "reminder",
                "target": "list",
                "value": ""
            }
        
        return None
    
    def _parse_clipboard(self, text: str) -> Optional[Dict[str, Any]]:
        """解析剪贴板命令"""
        if '剪贴板' in text:
            if '清空' in text or '清除' in text:
                return {
                    "command": "clipboard",
                    "target": "clear",
                    "value": ""
                }
            elif '显示' in text or '查看' in text:
                return {
                    "command": "clipboard",
                    "target": "show",
                    "value": ""
                }
        elif '复制' in text:
            # 提取要复制的内容
            content_match = re.search(r'复制\s*[:：]?\s*([^\s].+)', text)
            if content_match:
                content = content_match.group(1)
                return {
                    "command": "clipboard",
                    "target": "copy",
                    "value": content
                }
        elif '粘贴' in text:
            return {
                "command": "clipboard",
                "target": "paste",
                "value": ""
            }
        
        return None
    
    def _parse_text_input(self, text: str) -> Optional[Dict[str, Any]]:
        """解析文本输入命令"""
        if '输入' in text or '打字' in text:
            # 提取要输入的文本
            content_match = re.search(r'(输入|打字)\s*[:：]?\s*([^\s].+)', text)
            if content_match:
                content = content_match.group(2)
                return {
                    "command": "text_input",
                    "target": "type",
                    "value": content
                }
        elif '清空' in text and ('输入' in text or '文本' in text):
            return {
                "command": "text_input",
                "target": "clear",
                "value": ""
            }
        elif '删除' in text and ('文本' in text or '输入' in text):
            return {
                "command": "text_input",
                "target": "delete",
                "value": ""
            }
        
        return None
    
    def _parse_power_command(self, text: str) -> Optional[Dict[str, Any]]:
        """解析电源管理命令"""
        power_map = {
            '锁屏': {'target': 'lock', 'value': ''},
            '休眠': {'target': 'sleep', 'value': ''},
            # '关机': {'target': 'shutdown', 'value': '0'},  # 默认立即关机
            # '重启': {'target': 'restart', 'value': '0'},   # 默认立即重启
            '休眠': {'target': 'hibernate', 'value': ''}
        }
        
        # 提取延迟时间（分钟）
        time_match = re.search(r'(\d+)\s*分钟', text)
        delay_minutes = time_match.group(1) if time_match else "0"
        
        for keyword, config in power_map.items():
            if keyword in text:
                result = {
                    "command": "sys_power",
                    "target": config['target'],
                    "value": delay_minutes if config['target'] in ['shutdown', 'restart'] else config['value']
                }
                return result
        
        return None
    
    def _parse_file_operation(self, text: str) -> Optional[Dict[str, Any]]:
        """解析文件操作命令"""
        file_patterns = [
            r'创建\s*(文件|文件夹)\s*([^\s]+)',
            r'删除\s*(文件|文件夹)\s*([^\s]+)',
            r'复制\s*([^\s]+)\s*到\s*([^\s]+)',
            r'移动\s*([^\s]+)\s*到\s*([^\s]+)',
            r'重命名\s*([^\s]+)\s*为\s*([^\s]+)',
            r'查找\s*([^\s]+)',
            r'搜索\s*([^\s]+)'
        ]
        
        for pattern in file_patterns:
            match = re.search(pattern, text)
            if match:
                if '创建' in text:
                    target_type = match.group(1)
                    file_path = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "create",
                        "value": f"{target_type}:{file_path}"
                    }
                elif '删除' in text:
                    file_path = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "delete",
                        "value": file_path
                    }
                elif '复制' in text:
                    source = match.group(1)
                    dest = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "copy",
                        "value": f"{source}:{dest}"
                    }
                elif '移动' in text:
                    source = match.group(1)
                    dest = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "move",
                        "value": f"{source}:{dest}"
                    }
                elif '重命名' in text:
                    old_name = match.group(1)
                    new_name = match.group(2)
                    return {
                        "command": "file_operation",
                        "target": "rename",
                        "value": f"{old_name}:{new_name}"
                    }
                elif '查找' in text or '搜索' in text:
                    search_term = match.group(1)
                    return {
                        "command": "file_operation",
                        "target": "find",
                        "value": search_term
                    }
        
        return None
    
    def _parse_web_search(self, text: str) -> Optional[Dict[str, Any]]:
        """解析网络搜索命令"""
        search_patterns = [
            r'(百度|谷歌|必应)\s*搜索\s*([^\s].+)',
            r'搜索\s*([^\s].+)',
            r'查找\s*([^\s].+)'
        ]
        
        for pattern in search_patterns:
            match = re.search(pattern, text)
            if match:
                if '百度' in text:
                    search_term = match.group(2) if match.lastindex >= 2 else match.group(1)
                    return {
                        "command": "web_search",
                        "target": "baidu",
                        "value": search_term
                    }
                elif '谷歌' in text:
                    search_term = match.group(2) if match.lastindex >= 2 else match.group(1)
                    return {
                        "command": "web_search",
                        "target": "google",
                        "value": search_term
                    }
                elif '必应' in text:
                    search_term = match.group(2) if match.lastindex >= 2 else match.group(1)
                    return {
                        "command": "web_search",
                        "target": "bing",
                        "value": search_term
                    }
                else:
                    search_term = match.group(1)
                    return {
                        "command": "web_search",
                        "target": "baidu",
                        "value": search_term
                    }
        
        return None
    
    def _parse_media_control(self, text: str) -> Optional[Dict[str, Any]]:
        """解析媒体控制命令"""
        media_map = {
            '播放': 'play',
            '暂停': 'pause',
            '停止': 'stop',
            '下一首': 'next',
            '上一首': 'previous',
            '快进': 'forward',
            '快退': 'rewind',
            '全屏': 'fullscreen',
            '退出全屏': 'exit_fullscreen'
        }
        
        for keyword, action in media_map.items():
            if keyword in text:
                return {
                    "command": "media_control",
                    "target": action,
                    "value": ""
                }
        
        return None
    
    def _parse_window_management(self, text: str) -> Optional[Dict[str, Any]]:
        """解析窗口管理命令"""
        window_map = {
            '最大化': 'maximize',
            '最小化': 'minimize',
            '还原': 'restore',
            '关闭窗口': 'close_window',
            '切换': 'switch',
            '显示桌面': 'show_desktop',
            '桌面': 'show_desktop'
        }
        
        for keyword, action in window_map.items():
            if keyword in text:
                return {
                    "command": "window_management",
                    "target": action,
                    "value": ""
                }
        
        return None
    
    def _parse_network_operation(self, text: str) -> Optional[Dict[str, Any]]:
        """解析网络操作命令"""
        if 'wifi' in text:
            if '连接' in text:
                # 提取WiFi名称
                wifi_match = re.search(r'连接\s*wifi\s*([^\s]+)', text)
                wifi_name = wifi_match.group(1) if wifi_match else ""
                return {
                    "command": "network_operation",
                    "target": "wifi_connect",
                    "value": wifi_name
                }
            elif '断开' in text:
                return {
                    "command": "network_operation",
                    "target": "wifi_disconnect",
                    "value": ""
                }
        elif '蓝牙' in text:
            if '打开' in text or '开启' in text:
                return {
                    "command": "network_operation",
                    "target": "bluetooth_on",
                    "value": ""
                }
            elif '关闭' in text:
                return {
                    "command": "network_operation",
                    "target": "bluetooth_off",
                    "value": ""
                }
        elif '热点' in text:
            if '打开' in text or '开启' in text:
                return {
                    "command": "network_operation",
                    "target": "hotspot_on",
                    "value": ""
                }
            elif '关闭' in text:
                return {
                    "command": "network_operation",
                    "target": "hotspot_off",
                    "value": ""
                }
        
        return None
    
    def _parse_screenshot(self, text: str) -> Optional[Dict[str, Any]]:
        """解析截图命令"""
        if '截图' in text or '截屏' in text:
            if '区域' in text:
                return {
                    "command": "screenshot",
                    "target": "area",
                    "value": ""
                }
            elif '窗口' in text:
                return {
                    "command": "screenshot",
                    "target": "window",
                    "value": ""
                }
            else:
                return {
                    "command": "screenshot",
                    "target": "full",
                    "value": ""
                }
        
        return None
    
    def _parse_reminder(self, text: str) -> Optional[Dict[str, Any]]:
        """解析提醒命令"""
        if '提醒' in text or '闹钟' in text:
            # 提取时间和提醒内容
            time_match = re.search(r'(\d+)\s*(分钟|小时|秒)', text)
            content_match = re.search(r'提醒\s*[:：]?\s*([^\s].+)', text)
            
            if time_match:
                time_value = time_match.group(1)
                time_unit = time_match.group(2)
                content = content_match.group(1) if content_match else "提醒时间到"
                
                return {
                    "command": "reminder",
                    "target": "set",
                    "value": f"{time_value}:{time_unit}:{content}"
                }
            elif content_match:
                content = content_match.group(1)
                return {
                    "command": "reminder",
                    "target": "set",
                    "value": f"0:分钟:{content}"
                }
        elif '取消提醒' in text:
            return {
                "command": "reminder",
                "target": "cancel",
                "value": ""
            }
        elif '查看提醒' in text:
            return {
                "command": "reminder",
                "target": "list",
                "value": ""
            }
        
        return None
    
    def _parse_clipboard(self, text: str) -> Optional[Dict[str, Any]]:
        """解析剪贴板命令"""
        if '剪贴板' in text:
            if '清空' in text or '清除' in text:
                return {
                    "command": "clipboard",
                    "target": "clear",
                    "value": ""
                }
            elif '显示' in text or '查看' in text:
                return {
                    "command": "clipboard",
                    "target": "show",
                    "value": ""
                }
        elif '复制' in text:
            # 提取要复制的内容
            content_match = re.search(r'复制\s*[:：]?\s*([^\s].+)', text)
            if content_match:
                content = content_match.group(1)
                return {
                    "command": "clipboard",
                    "target": "copy",
                    "value": content
                }
        elif '粘贴' in text:
            return {
                "command": "clipboard",
                "target": "paste",
                "value": ""
            }
        
        return None
    
    def _parse_text_input(self, text: str) -> Optional[Dict[str, Any]]:
        """解析文本输入命令"""
        if '输入' in text or '打字' in text:
            # 提取要输入的文本
            content_match = re.search(r'(输入|打字)\s*[:：]?\s*([^\s].+)', text)
            if content_match:
                content = content_match.group(2)
                return {
                    "command": "text_input",
                    "target": "type",
                    "value": content
                }
        elif '清空' in text and ('输入' in text or '文本' in text):
            return {
                "command": "text_input",
                "target": "clear",
                "value": ""
            }
        elif '删除' in text and ('文本' in text or '输入' in text):
            return {
                "command": "text_input",
                "target": "delete",
                "value": ""
            }
        
        return None

# 使用示例
def main():
    parser = CommandParser()
    
    # 测试用例
    test_commands = [
        "打开chrome",
        "关闭word",
        "显示ip",
        "音量调大",
        "亮度调暗20%",
        "关机10分钟",
        "播放音乐",
        "静音",
        "锁屏",
        "打开浏览器",
        "关闭笔记本",
        "启动微信",
        "退出qq",
        # 新增的测试用例
        "创建文件test.txt",
        "删除文件old.txt",
        "复制file.txt到backup",
        "移动document到folder",
        "重命名old.txt为new.txt",
        "查找Python文件",
        "百度搜索人工智能",
        "谷歌搜索机器学习",
        "播放",
        "暂停",
        "下一首",
        "全屏",
        "最大化窗口",
        "最小化窗口",
        "显示桌面",
        "连接wifi MyWiFi",
        "断开wifi",
        "打开蓝牙",
        "关闭蓝牙",
        "截图",
        "区域截图",
        "窗口截图",
        "提醒5分钟:开会时间到了",
        "设置闹钟10秒:测试提醒",
        "取消提醒",
        "查看提醒",
        "复制:这是要复制的文本",
        "粘贴",
        "清空剪贴板",
        "查看剪贴板",
        "输入:Hello World",
        "清空输入",
        "删除文本",
        "无效指令"
    ]
    
    for cmd in test_commands:
        result = parser.parse_command(cmd)
        print(f"指令: '{cmd}'")
        print(f"解析结果: {json.dumps(result, ensure_ascii=False, indent=2)}")
        print("-" * 50)

if __name__ == "__main__":
    main()